package com.china08.yxyapi.api.home.activity.marker;

import java.text.ParseException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.maker.YxyMakerWorks;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.marker.YxyMakerWorksRepository;
import com.china08.yxyapi.security.profile.YxyProfile;

/**
 * 区域教育局-竞赛报名-创建
 * 
 */
@CrossOrigin
@RestController
public class YxyMakerWorksCrtController extends YxyApiBaseController {

    @Autowired
    private YxyMakerWorksRepository yxyMakerWorksRepository;

    @ResponseBody
    @RequestMapping(value = "/api_yw/home/activity/competition/catalog/works/crt", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel)
            throws ParseException {
        // 得到当前登录用户
        YxyProfile profile = getProfile(request, response);
        String schoolId = profile.getSchoolId();
        String schoolName = profile.getSchoolName();
        String code = profile.getSchoolAreaIdDistrict();
        YxyMakerWorks works = new YxyMakerWorks();
        works.setCatalog1(reqModel.catalog1);
        works.setCatalog2(reqModel.catalog2);
        works.setCatalogname1(reqModel.catalogname1);
        works.setCatalogname2(reqModel.catalogname2);
        works.setDistrictCode(code);
        works.setName(reqModel.name);
        works.setPhase(reqModel.phase);
        works.setRegisterFile(reqModel.registerFile);
        works.setWorksFile(reqModel.worksFile);
        works.setRegisterFileName(reqModel.regFileName);
        works.setRegistermd5(reqModel.regmd5);
        works.setWorksFileName(reqModel.workFileName);
        works.setWorkmd5(reqModel.workmd5);
        works.setSchoolId(schoolId);
        works.setSchoolName(schoolName);
        yxyMakerWorksRepository.save(works);
        RespModel respModel = new RespModel();
        respModel.notice = 1;
        return respModel;
    }

    public static class ReqModel extends YxyApiReqModel {
        // 所属学段 小学：1 初中：2 高中：3 中职：4
        public String phase;
        // 作品分类id 全部：all
        public String catalog1;
        // 项目名称id 全部：all
        public String catalog2;
        // 作品分类name 全部：all
        public String catalogname1;
        // 项目名称name 全部：all
        public String catalogname2;
        // 作品名称
        public String name;
        // 报名文件
        public String registerFile;
        // 作品文件
        public String worksFile;
        // 报名文件名
        public String regFileName;
        // 作品文件名
        public String workFileName;
        // 报名文件md5
        public String regmd5;
        // 作品文件md5
        public String workmd5;
    }

    protected static class RespModel extends YxyApiRespModel {
        public Integer notice;
    }

}
